import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/home/home_item_info.dart';
import 'package:xiongan_app_yy/provider/fab_new_provider.dart';

class HomeSinglePage extends StatefulWidget {
  @override
  _HomeSinglePageState createState() => _HomeSinglePageState();
  String name;

  HomeSinglePage(this.name);
}

class _HomeSinglePageState extends State<HomeSinglePage> {
  List<InvListData> dataList = List();
  int pos;

  @override
  void initState() {
    if (widget.name == "出库预约") {
      pos = 0;
      getData();
    } else if (widget.name == "配送预约") {
      pos = 1;
      getDeliveryData();
    } else if (widget.name == "结余退库") {
      pos = 2;
      getReturnData();
    } else if (widget.name == "入库预约") {
      pos = 3;
      getInData();
    } else if (widget.name == "调配预约") {
      pos = 4;
      getDepData();
    }
    WidgetsBinding.instance.addPostFrameCallback(
        (_) => Provider.of<FabNewProvider>(context).addListener(() {
              int position = Provider.of<FabNewProvider>(context).index;
              if (position == 0) {
                getData();
              } else if (position == 1) {
                getDeliveryData();
              } else if (position == 2) {
                getReturnData();
              } else if (position == 3) {
                getInData();
              } else if (position == 4) {
                getDepData();
              }
            }));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 2,
                height: 16,
                margin: EdgeInsets.only(left: 10),
                decoration: BoxDecoration(
                    border: Border.all(color: Color(0xFFF87337), width: 1)),
              ),
              Container(
                padding: EdgeInsets.only(top: 10, bottom: 10, left: 10),
                child: Text(
                  widget.name,
                  style: TextStyle(color: Color(0xff323232), fontSize: 16),
                ),
              ),
            ],
          ),
          Divider(height: 1, color: Color(0xffeeeeee)),
          Expanded(
              child: RefreshIndicator(
            child: ListView.separated(
              itemCount: dataList.length,
              padding: EdgeInsets.only(top: 0),
              itemBuilder: (context, index) {
                if (pos == 0) {
                  return HomeItemInfo(dataList[index], pos, getData);
                } else if (pos == 1) {
                  return HomeItemInfo(dataList[index], pos, getDeliveryData);
                } else if (pos == 2) {
                  return HomeItemInfo(dataList[index], pos, getReturnData);
                } else if (pos == 3) {
                  return HomeItemInfo(dataList[index], pos, getInData);
                } else {
                  return HomeItemInfo(dataList[index], pos, getDepData);
                }
              },
              separatorBuilder: (context, index) {
                return Divider(height: 2, color: Color(0xffeeeeee));
              },
            ),
            onRefresh: _onRefresh,
          ))
        ],
      ),
    );
  }

  void getData() {
    dataList.clear();
    InternetAPI.getInstance()
        .get("/OMS/MiWarehouseOutTask/GetAllMiWarehouseOutTask", (data) {
      setState(() {
        dataList.addAll(InvListEntity.fromJson(data).datas);
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "Warehouseouttype": "10",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  void getDeliveryData() {
    dataList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiWarehouseOutTask/GetAllMiWarehouseOutTask", (data) {
      setState(() {
        dataList.addAll(InvListEntity.fromJson(data).datas);
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "Warehouseouttype": "20",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  void getReturnData() {
    dataList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiBackWarehouseTask/GetAllMiBackWarehouseTask", (data) {
      setState(() {
        dataList.addAll(InvListEntity.fromJson(data).datas);
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "userid": DataSingleton().Data.id,
      "Warehouseouttype": "20",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  void getInData() {
    dataList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiWarehouseInTask/GetAllMiWarehouseInTask", (data) {
      setState(() {
        dataList.addAll(InvListEntity.fromJson(data).datas);
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "appointmentmode": "20",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  void getDepData() {
    dataList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiAllocationTaskDetail/GetAllMiAllocationTaskDetail", (data) {
      setState(() {
        dataList.addAll(InvListEntity.fromJson(data).datas);
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "appointmentmode": "20",
      "pageNo": 1,
      "pageSize": 5
    });
  }

  Future<void> _onRefresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      return null;
    });
    if (pos == 0) {
      getData();
    } else if (pos == 1) {
      getDeliveryData();
    } else if (pos == 2) {
      getReturnData();
    } else if (pos == 3) {
      getInData();
    } else if (pos == 4) {
      getDepData();
    }
  }
}
